prec <- function(x, 
                 date, 
                 first = date[1],
                 last = date[length(date)], 
                 cond, 
                 type = c("line", "hist", "both"), 
                 ...) {
################################################################################
##  
##  This program plots a time series variable either as a time line, a histogram
##  or both. It i possible to pass further standard lattice arguments. 
##  
##  parameters are as follows:
##  x = object to be plotted (no default)
##  date = the date variable from the input time series dataframe (no default)
##  first = starting date of the plot (format: "%Y-%m-%d %H:%M:%S")
##          (default is first of date)
##  last = same as first for closing date
##  cond = conditioning variable (optional - no default)
##  type = plot type
##         character string: one of "line" (time line), "hist" (histogram), "both"
##
##  no default settings
##
################################################################################
##
##  Copyright (C) 2011 Steffi Instinsky, Simon Lange, Melanie Schnelle
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##  Please send any comments, suggestions, criticism, or (for our sake) bug
##  reports to eimar.teaching@gmail.com
##
################################################################################
 
## set system locale time zone to "UTC" for time handling w/out
## daylight saving
Sys.setenv(TZ = "UTC")
  
  
  ## convert date variable and 'first' and 'last' parameter to POSIX
  date <- as.POSIXct(date)
  first <- as.POSIXct(first)
  last <- as.POSIXct(last)
                      
 ## revert system local time zone setting to original
  Sys.setenv(TZ = "CET")

  ## define line plot object
  l <- xyplot(x ~ date | cond, type = "l", as.table = T,
              subset = date > first & date < last & cond != "", 
              layout = c(1, 2),
              xlim = c(first, last), ...)
  
  ## define histogram plot object
  h <- histogram(~ x | cond, subset = date > first & date < last & cond != "",
                 layout = c(1,2), as.table = T, ...)
  
  ## define combination plot object
  b <- c(h, l, layout = c(2, 2))
  
  ## define out plot object conditional on what type is passed from function call
  out <- switch(type,
                "line" = l,
                "hist" = h,
                "both" = b)

  return(print(out))
  
}

## Example
data <- read.table("C:/Users/Melanie/Desktop/RKurs/Precipitation/dataset_precipitation.dat", 
                   fill = T, sep = ",", header = T)

## load necessary packages
library(latticeExtra)


#prec(dat$t_wxt, dat$dateEAT, cond = dat$plotID, type = "both")

prec(x = data$precip_wxt, 
            date = data$dateEAT, 
            first = "2011-04-01 00:00:00",
            last = "2011-04-30 23:55:00", 
            cond = data$plotID,
            type = "both")
            
 